home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / collectn / seq.jav < prev    next >
Encoding:
Text File  |  1995-10-14  |  6.1 KB  |  192 lines

  1. /*
  2.   File: Seq.java
  3.  
  4.   Originally written by Doug Lea and released into the public domain. 
  5.   Thanks for the assistance and support of Sun Microsystems Labs, Agorics 
  6.   Inc, Loral, and everyone contributing, testing, and using this code.
  7.  
  8.   History:
  9.   Date     Who                What
  10.   24Sep95  dl@cs.oswego.edu   Create from collections.java  working file
  11.  
  12. */
  13.   
  14. package collections;
  15.  
  16. import java.util.Enumeration;
  17. import java.util.NoSuchElementException;
  18.  
  19. /**
  20.  * 
  21.  *
  22.  * Seqs are indexed, sequentially ordered collections.
  23.  * Indices are always in the range 0 .. size() -1. All accesses by index
  24.  * are checked, raising exceptions if the index falls out of range.
  25.  * <P>
  26.  * The elements() enumeration for all seqs is guaranteed to be
  27.  * traversed (via nextElement) in sequential order.
  28.  * @author Doug Lea
  29.  * @version 0.93
  30.  *
  31.  * <P> For an introduction to this package see <A HREF="index.html"> Overview </A>.
  32. **/
  33.  
  34. public interface Seq extends Collection {
  35.  
  36. /**
  37.  * Return the element at the indicated index
  38.  * @param index 
  39.  * @return the element at the index
  40.  * @exception NoSuchElementException if index is not in range 0..size()-1
  41. **/
  42.  
  43.   public Object      at(int index) 
  44.                        throws  NoSuchElementException;
  45.  
  46. /**
  47.  * Return the first element, if it exists.
  48.  * Behaviorally equivalent to at(0)
  49.  * @exception NoSuchElementException if isEmpty
  50. **/
  51.  
  52.   public Object      first() 
  53.                        throws NoSuchElementException;
  54.  
  55. /**
  56.  * Return the last element, if it exists.
  57.  * Behaviorally equivalent to at(size()-1)
  58.  * @exception NoSuchElementException if isEmpty
  59. **/
  60.  
  61.   public Object      last() 
  62.                        throws NoSuchElementException;
  63.  
  64. /**
  65.  * Report the index of leftmost occurrence of an element from a 
  66.  * given starting point, or -1 if there is no such index.
  67.  * @param element the element to look for
  68.  * @param startingIndex the index to start looking from. The startingIndex
  69.  * need not be a valid index. If less than zero it is treated as 0.
  70.  * If greater than or equal to size(), the result will always be -1.
  71.  * @return index such that
  72.  * <PRE> 
  73.  * let int si = max(0, startingIndex) in
  74.  *  index == -1 &&
  75.  *   foreach (int i in si .. size()-1) !at(index).equals(element)
  76.  *  ||
  77.  *  at(index).equals(element) &&
  78.  *   foreach (int i in si .. index-1) !at(index).equals(element)
  79.  * </PRE>
  80. **/
  81.  
  82.   public int         firstIndexOf(Object element, int startingIndex);
  83.  
  84. /**
  85.  * Find the leftmost occurrence of an element.
  86.  * Behaviorally equivalent to firstIndexOf(element, 0)
  87. **/
  88.    
  89.   public int         firstIndexOf(Object element);
  90.  
  91. /**
  92.  * Report the index of righttmost occurrence of an element from a 
  93.  * given starting point, or -1 if there is no such index.
  94.  * @param element the element to look for
  95.  * @param startingIndex the index to start looking from. The startingIndex
  96.  * need not be a valid index. If less than zero the result
  97.  * will always be -1.
  98.  * If greater than or equal to size(), it is treated as size()-1.
  99.  * @return index such that
  100.  * <PRE> 
  101.  * let int si = min(size()-1, startingIndex) in
  102.  *  index == -1 &&
  103.  *   foreach (int i in 0 .. si) !at(index).equals(element)
  104.  *  ||
  105.  *  at(index).equals(element) &&
  106.  *   foreach (int i in index+1 .. si) !at(index).equals(element)
  107.  * </PRE>
  108.  *
  109. **/
  110.   public int         lastIndexOf(Object element, int startingIndex);
  111.  
  112.  
  113. /**
  114.  * Find the rightmost occurrence of an element.
  115.  * Behaviorally equivalent to lastIndexOf(element, size()-1)
  116. **/
  117.   public int         lastIndexOf(Object element);
  118.  
  119. /**
  120.  * Construct a new Seq that is a clone of self except
  121.  * that it does not contain the elements before index or
  122.  * after index+length. If length is less than or equal to zero,
  123.  * return an empty Seq.
  124.  * @param index of the element that will be the 0th index in new Seq
  125.  * @param length the number of elements in the new Seq
  126.  * @return new seq such that
  127.  * <PRE>
  128.  * s.size() == max(0, length) &&
  129.  * foreach (int i in 0 .. s.size()-1) s.at(i).equals(at(i+index)); 
  130.  * </PRE>
  131.  * @exception NoSuchElementException if index is not in range 0..size()-1
  132. **/
  133.   public Seq         subseq(int index, int length) 
  134.                         throws NoSuchElementException;
  135.  
  136. /**
  137.  * Construct a new Seq that is a clone of self except
  138.  * that it adds (inserts) the indicated element at the
  139.  * indicated index.
  140.  * @param index the index at which the new element will be placed
  141.  * @param element The element to insert in the new collection
  142.  * @return new seq s, such that
  143.  * <PRE>
  144.  *  s.at(index) == element &&
  145.  *  foreach (int i in 1 .. s.size()-1) s.at(i).equals(at(i-1));
  146.  * </PRE>
  147.  * @exception NoSuchElementException if index is not in range 0..size()-1
  148. **/
  149.  
  150.   public Seq  insertingAt(int index, Object element) 
  151.                         throws IllegalElementException,
  152.                                NoSuchElementException;
  153.  
  154. /**
  155.  * Construct a new Seq that is a clone of self except
  156.  * that the indicated element is placed at the indicated index.
  157.  * @param index the index at which to replace the element
  158.  * @param element The new value of at(index)
  159.  * @return new seq, s, such that
  160.  * <PRE>
  161.  *  s.at(index) == element &&
  162.  *  foreach (int i in 0 .. s.size()-1) 
  163.  *     (i != index) --> s.at(i).equals(at(i));
  164.  * </PRE>
  165.  * @exception NoSuchElementException if index is not in range 0..size()-1
  166. **/
  167.  
  168.   public Seq   replacingAt(int index, Object element) 
  169.                         throws IllegalElementException,
  170.                                NoSuchElementException;
  171.  
  172. /**
  173.  * Construct a new Seq that is a clone of self except
  174.  * that it does not contain the element at the indeicated index; all
  175.  * elements to its right are slided left by one.
  176.  *
  177.  * @param index the index at which to remove an element
  178.  * @return new seq such that
  179.  * <PRE>
  180.  *  foreach (int i in 0.. index-1) s.at(i).equals(at(i)); &&
  181.  *  foreach (int i in index .. s.size()-1) s.at(i).equals(at(i+1));
  182.  * </PRE>
  183.  * @exception NoSuchElementException if index is not in range 0..size()-1
  184. **/
  185.   public Seq   removingAt(int index) 
  186.                         throws NoSuchElementException;
  187.  
  188.  
  189. }
  190.  
  191.  
  192.